//
// RS Game Framework
// Copyright © 2009 Jedd Haberstro
// jhaberstro@gmail.com
// 
// $Id:
//

#include "rs/unittest/ConsoleTestReporter.hpp"
#include "rs/unittest/TestCase.hpp"
#include "rs/unittest/TestFailure.hpp"
#include "rs/unittest/TestSuite.hpp"
#include <cstdio>
#include <string>

namespace rs
{
    namespace unittest
    {
        void ConsoleTestReporter::ReportSuiteStart(TestSuite const* suite) {
            std::fprintf(stderr, "Starting suite '%s': %i tests.....\n", suite->GetName(), suite->GetTestCount());
            std::fprintf(stderr, "-------------------------------------------------------------------------\n");
        }
        
        void ConsoleTestReporter::ReportSuiteEnd(TestSuite const* suite) {
            std::fprintf(stderr, "\n");
        }
        
        void ConsoleTestReporter::ReportTestSuccees(TestCase const* test) {
            std::fprintf(stderr, "\t%s.... SUCCESS\n", test->GetTestName());
        }
        
        void ConsoleTestReporter::ReportTestFailure(TestCase const* test, TestFailure const* failureDetails) {
            std::fprintf(stderr, "\t%s.... FAILURE\n", test->GetTestName());
            
            // "%s:%d: error: Failure in %s: %s\n%s", failureDetails->file, failureDetails->line, test->GetTestName(), failureDetails->message, failureDetails->condition
            Char lineCString[21];
            std::sprintf(lineCString, "%d", failureDetails->line);
            std::string failureMessage;
            failureMessage.append(failureDetails->file);
            failureMessage.append(":");
            failureMessage.append(lineCString);
            failureMessage.append(":\nFailure in suite-test: '");
            failureMessage.append(test->GetSuiteName());
            failureMessage.append("::");
            failureMessage.append(test->GetTestName());
            failureMessage.append("'\n");
            failureMessage.append("Message: ");
            failureMessage.append(failureDetails->message);
            failureMessage.append("\nCondition: ");
            failureMessage.append(failureDetails->condition);
            failureMessages_.push_back(failureMessage);
        }
        
        void ConsoleTestReporter::ReportSummary(unsigned int totalTests, unsigned int failedTests) {
            if (failedTests > 0) {
                std::fprintf(stderr, "=========================================================================\n");

                for (FailureMessageContainer::const_iterator itr = failureMessages_.begin(); itr != failureMessages_.end(); ++itr) {
                    std::fprintf(stderr, "-------------------------------------------------------------------------\n");
                    std::fprintf(stderr, itr->c_str());
                    std::fprintf(stderr, "\n\n");
                }

                std::fprintf(stderr, "=========================================================================\n");
            }
            else {
                std::fprintf(stderr, "=========================================================================\n");
            }

            std::fprintf(stderr, "Total tests: %d\n", totalTests);
            std::fprintf(stderr, "Total successful tests: %d\n", totalTests - failedTests);
            std::fprintf(stderr, "Total failed tests: %d\n", failedTests);
            
            failureMessages_.clear();
        }
    }
}
